let movieDao=require("../dao/movieDao.js");
let ResultUtil=require("../util/ResultUtil.js");

// 分页显示
async function list(filter,page,limit){
    let data=await movieDao.getMovies(filter,page,limit);
    let count=await movieDao.movieCount(filter);
    let result=ResultUtil(data,count);
    return result;
}

// 增加电影
async function addMovie(movie){
    // 强行赋值
movie.director=[
    {
        name:movie.directorName,
        image:movie.image,
    }
];
movie.actor=[
    {
        name:movie.actorName,
        filmName:movie.filmName,
        image:movie.image,
    }
];
movie.images=[
  {image:movie.image,} 
]

    let data=await movieDao.addMovie(movie);
    if(data){
        return {code:200,msg:"添加成功"}
    }else{
        return {code:-1,msg:"添加失败"}
    }
}

// 删除电影
async function deleteMovie(id){
    let data=await movieDao.deleteMovie(id);
    if(data.deletedCount>=1){
        return {code:200,msg:"删除成功"}
    }else{
        return{code:-1,msg:"删除失败"}
    }
}

// 更新电影
async function updateMovie(movie){
    if(!movie||!movie._id)return null;

    let data=await movieDao.updateMovie(movie);
    return data;

}

module.exports.list=list;
module.exports.addMovie=addMovie;
module.exports.deleteMovie=deleteMovie;
module.exports.updateMovie=updateMovie;


